package cn.com.demo.i18n.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
public class ASyncService implements InitializingBean {
    @Autowired
    Executor myExecutor;
    @Async
    public void do1() {
        try {
            log.info("do1开始执行");
            Thread.currentThread().setName("do1-"+ Thread.currentThread().getId());
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error("发生异常",e);
        } finally {
            log.info("do1执行结束"+"	"+Thread.currentThread().getName() + " "+Thread.currentThread().getId());
        }
    }
    @Async
    public Future<String> do2() {
        try {
            log.info("do2开始执行");
            Thread.currentThread().setName("do2-"+ Thread.currentThread().getId());
            Thread.sleep(2000);
            //
            return new AsyncResult<String>(UUID.randomUUID().toString());
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error("发生异常",e);
        } finally {
            log.info("do2执行结束"+"	"+Thread.currentThread().getName() + " "+Thread.currentThread().getId());
        }
        return AsyncResult.forValue("no data");
    }
    @Async("myExecutor")
    public void do3() {
        try {
            log.info("do3开始执行");
            Thread.currentThread().setName("do3-"+ Thread.currentThread().getId());
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error("发生异常",e);
        } finally {
            log.info("do3执行结束"+"	"+Thread.currentThread().getName() + " "+Thread.currentThread().getId());
        }
    }
    @Async("myExecutor")
    public void do4() {
        try {
            log.info("do4开始执行");
            Thread.currentThread().setName("do4-"+ Thread.currentThread().getId());
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error("发生异常",e);
        } finally {
            log.info("do4执行结束"+"	"+Thread.currentThread().getName() + " "+Thread.currentThread().getId());
        }
    }
    @Async("myExecutor")
    public void do5() {
        try {
            log.info("do5开始执行");
            Thread.currentThread().setName("do5-"+ Thread.currentThread().getId());
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error("发生异常",e);
        } finally {
            log.info("do5执行结束"+"	"+Thread.currentThread().getName() + " "+Thread.currentThread().getId());
        }
    }
    @Async("myExecutor")
    public void do6() {
        try {
            log.info("do6开始执行");
            Thread.currentThread().setName("do6-"+ Thread.currentThread().getId());
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error("发生异常",e);
        } finally {
            log.info("do6执行结束"+"	"+Thread.currentThread().getName() + " "+Thread.currentThread().getId());
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        ThreadPoolExecutor myExecutor = (ThreadPoolExecutor) this.myExecutor;
        new Thread(()->{
//            Thread.currentThread().setName(Thread.currentThread().getId()+"count-thread-sssss");
            while (true) {
                log.info("任务数量:{},compateCount:{},active:{},coreSize:{},poolSize{},queueSize:{}", myExecutor.getTaskCount(),myExecutor.getCompletedTaskCount(),myExecutor.getActiveCount(),myExecutor.getCorePoolSize(),myExecutor.getPoolSize(),myExecutor.getQueue().size());


                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"count-thread").start();
    }
}
